package com.iman.manjusri.service.impl;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.iman.manjusri.service.FreeMarkerService;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreeMarkerServiceImpl implements FreeMarkerService {
    
    FreeMarkerConfigurer freemarkerConfig;
    
    @Override
    public String process2String(String templateId, Map params) {
        StringWriter writer = new StringWriter();
        try {
            Template template = freemarkerConfig.getConfiguration().getTemplate(templateId + ".ftl");
            template.process(params, writer);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return writer.toString();
    }

    public FreeMarkerConfigurer getFreemarkerConfig() {
        return freemarkerConfig;
    }

    public void setFreemarkerConfig(FreeMarkerConfigurer freemarkerConfig) {
        this.freemarkerConfig = freemarkerConfig;
    }

}
